<?php
namespace app\passenger\controller;
use app\AdminRest;
use app\ApiRest;
use app\card\model\DefaultSetting;
use app\passenger\info\PermissionPassenger;
use app\shop\model\IndexCardCount;
use think\App;
use app\passenger\model\IndexActive as Model;
use app\passenger\model\AdminCompany;
use app\passenger\model\Coupon;
use app\passenger\model\Log;


use think\facade\Cache;
use think\facade\Db;

class IndexActive extends ApiRest
{

    protected $model;

    protected $company_model;

    protected $coupon_model;

    protected $log_model;

    protected $count_model;


    public function __construct(App $app) {
        parent::__construct($app);
        $this->model= new Model();

        $this->company_model= new AdminCompany();

        $this->coupon_model = new Coupon();

        $this->log_model    = new Log();

        $this->count_model  = new IndexCardCount();
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-12 17:56
     * @功能说明:优惠券详情
     */
    public function activeInfo(){

        $input      = $this->_param;
        //活动id
        $id         = !empty($input['id'])?$input['id']:0;
        //员工id
        $staff_id   = !empty($input['staff_id'])?$input['staff_id']:0;
        //分享人
        $share_user = !empty($input['share_user'])?$input['share_user']:$staff_id;
        //活动信息
        $data['info']  = $this->model->activeInfo($staff_id,$this->_uniacid,$id,'a.*',1);

        if(empty($data['info'])){

            return $this->error('活动未找到');
        }

        $dis = [

            'uniacid'   => $this->_uniacid,

            'user_id'   => $this->getUserId(),

            'active_id' => $data['info']['id'],

            'type'      => 1,
        ];
        //查找是否领取过
        $coupon = $this->coupon_model->where($dis)->find();

        $all_dis = [

            'type' => 1,

            'active_id' => $data['info']['id'],

            'uniacid'   => $this->_uniacid,
        ];

        //是否领取完
        $all_coupon_num = $this->coupon_model->where($all_dis)->count();

        if(!empty($coupon)){
            //已经领取
            $data['info']['active_status'] = 2;

        }elseif($data['info']['end_time']<time()){
            //已经结束
            $data['info']['active_status'] = 3;

        }elseif($data['info']['start_time']>time()){
            //未开始
            $data['info']['active_status'] = 4;

        }elseif ($data['info']['user_coupon_num']<=$all_coupon_num){
            //已经领完
            $data['info']['active_status'] = 5;

        }else{
            //进行中
            $data['info']['active_status'] = 1;
        }

        $all_dis['share_user'] = $this->getUserId();
        //判断当前用户是否是员工
        $is_staff = Db::name('longbing_card_user_info')->where(['fans_id'=>$this->getUserId(),'is_staff'=>1])->field('id')->find();
        //领取数量
        $data['info']['all_coupon_num']    = $this->coupon_model->where($all_dis)->count();

        $data['info']['all_coupon_num']    = ($data['info']['all_coupon_num']>$data['info']['coupon_num']&&empty($is_staff))?$data['info']['coupon_num']:$data['info']['all_coupon_num'];

        $data['info']['create_time']       = date('Y-m-d H:i:s',$data['info']['create_time']);

        $data['info']['end_time']          = date('Y-m-d H:i:s',$data['info']['end_time']);

        $data['info']['coupon_start']      = date('Y-m-d H:i:s',$data['info']['coupon_start']);

        $data['info']['coupon_end']        = date('Y-m-d H:i:s',$data['info']['coupon_end']);

        $data['info']['user_coupon_start'] = date('Y-m-d H:i:s',$data['info']['user_coupon_start']);

        $data['info']['user_coupon_end']   = date('Y-m-d H:i:s',$data['info']['user_coupon_end']);

        $data['info']['start_time']        = date('Y-m-d H:i:s',$data['info']['start_time']);

        if(!empty($input['share_status'])){
            //初始化一下
            $this->log_model->initToday();

            $log_dis = [

                'staff_id' => $input['staff_id'],

                'active_id'=> $data['info']['id'],

                'user_id'  => $share_user,

                'uniacid'  => $this->_uniacid,
            ];
            //添加浏览量
             $this->log_model->logInset($log_dis,'pv');
            //今日浏览量
             $this->log_model->logInset($log_dis,'pv_today_num');
            //上报雷达
            if(!empty($staff_id)){
                $inset_data = [
                    'user_id' => $this->getUserId(),
                    'to_uid'  => $staff_id,
                    'type'    => 555,
                    'sign'    => 'view',
                    'target'  => $id,
                    'uniacid' => $this->_uniacid
                ];
                $insert_id = $this->count_model->countAdd($inset_data);
                $push_data = array(
                    'action'  => 'longbingSendWxServiceNotice',
                    'event'   => 'longbingSendWxServiceNotice',
                    'count_id'=> $insert_id
                );
                publisher(json_encode($push_data ,true));
            }
        }
        //弹幕
        $data['dm_list'] = $this->coupon_model->couponDm($id);


        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 14:26
     * @功能说明:获取正在进行中的活动
     */
    public function getActiveIng(){

        $input = $this->_param;

        $per  = new PermissionPassenger($this->_uniacid);

        $auth = $per->pAuth();

        if($auth!=true){

            return $this->success([]);

        }
        //优惠券信息
        $data['active_id']  = $this->model->activeInfo($input['staff_id'],$this->_uniacid,0,'a.id');
        //默认配置模型
        $default = new DefaultSetting();
        //新的配置
        $setting = $default->settingInfo(['uniacid' => $this->_uniacid]);
        //活动的图
        $data['passenger_cover'] = $setting['passenger_cover'];

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-22 20:54
     * @功能说明:员工端活动列表
     */
    public function staffActiveList(){

        $input = $this->_param;
        //优惠券信息
        $data  = $this->model->staffActiveList($this->getUserId(),$this->_uniacid,$input['status']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 15:57
     * @功能说明:员工端 活动详情
     */
    public function staffActiveInfo(){
        //初始化每天的数据
        $this->log_model->initToday();

        $input = $this->_param;

        $input['staff_id'] = $this->getUserId();

        $dis = [

            'id' => $input['active_id'],
        ];
        //活动详情
        $data['info'] = $this->model->activeDetail($dis);

        $data['info']['start_time'] = date('Y-m-d H:i:s', $data['info']['start_time']);

        $data['info']['end_time']   = date('Y-m-d H:i:s', $data['info']['end_time']);
        //今天0时
        $f_time = strtotime(date('Y-m-d',time()));
        //今天24时
        $l_time = $f_time+86400;

        $log_dis[] = ['staff_id','=',$input['staff_id']];

        $log_dis[] = ['active_id','=',$input['active_id']];

        $log_dis[] = ['type','=',1];
        //领取数量
        $data['record']['get_num'] = $this->coupon_model->where($log_dis)->count();

        $log_dis[] = ['status','=',2];
        //使用数量
        $data['record']['use_num'] = $this->coupon_model->where($log_dis)->count();

        $log_dis[] = ['update_time','between',"$f_time,$l_time"];
        //今日领取数量
        $data['record']['today_use_num'] = $this->coupon_model->where($log_dis)->count();

        unset($log_dis[3]);
        unset($log_dis[4]);
        $log_dis[] = ['create_time','between',"$f_time,$l_time"];

        //今日领取数量
        $data['record']['today_get_num'] = $this->coupon_model->where($log_dis)->count();


        $dis_log = [

            'staff_id' => $input['staff_id'],

            'active_id'=> $input['active_id']
        ];
        //总的获取浏览量
        $data['record']['pv'] = $this->log_model->where($dis_log)->sum('pv');
        //总的分享量
        $data['record']['share_num'] = $this->log_model->where($dis_log)->sum('share_num');
        //今日浏览量
        $data['record']['pv_today'] = $this->log_model->where($dis_log)->sum('pv_today_num');
        //今日分享量
        $data['record']['share_today_num'] = $this->log_model->where($dis_log)->sum('share_today_num');

        return $this->success($data);


    }









}
